package com.hmall.order.mq;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmall.feign.client.ItemClient;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @Author lyh
 * @Date 2024/11/21 20:31
 */
@Component
public class Listener {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private ItemClient itemClient;
    @RabbitListener(bindings =@QueueBinding(value = @Queue(name = "delay.queue"),exchange =@Exchange(name="delay.exchange",delayed = "true"),key ="delay.key"))
    public void delayMessage(String  msg){
        System.out.println("延迟交换机，收到的消息:"+msg);
        Long orderId = Long.valueOf(msg);
        Order order = orderMapper.selectById(orderId);
        if(order.getStatus()==1){
            LambdaQueryWrapper<OrderDetail> wrapper = Wrappers.<OrderDetail>lambdaQuery().eq(OrderDetail::getOrderId, orderId);
            OrderDetail orderDetail = orderDetailMapper.selectOne(wrapper);
            itemClient.recoverStock(Long.valueOf(orderDetail.getItemId()),orderDetail.getNum());
            order.setStatus(5);
            orderMapper.updateById(order);
        }
    }
}
